package com.example.redis.myJredis;

import redis.clients.jedis.*;

import java.util.LinkedList;
import java.util.List;
import java.util.ResourceBundle;

/**
 * Created by SOAL on 2017/6/2 0002.
 */
public class JredisShare {

    public static void main(String[] args) {

        ResourceBundle bundle = ResourceBundle.getBundle("application-dev");
        if (bundle == null) {
            throw new IllegalArgumentException("[redis.properties] is not found!");
        }
        JedisPoolConfig config = new JedisPoolConfig();
        config.setMaxTotal(Integer.valueOf(bundle.getString("redis.pool.maxActive")));
        config.setMaxIdle(Integer.valueOf(bundle.getString("redis.pool.maxIdle")));
        config.setMaxWaitMillis(Long.valueOf(bundle.getString("redis.pool.maxWait")));
        config.setTestOnBorrow(Boolean.valueOf(bundle.getString("redis.pool.testOnBorrow")));
        config.setTestOnReturn(Boolean.valueOf(bundle.getString("redis.pool.testOnReturn")));




        JedisShardInfo jedisShardInfo1 = new JedisShardInfo(
                bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port")));
//        JedisShardInfo jedisShardInfo2 = new JedisShardInfo(
//                bundle.getString("redis.ip"), Integer.valueOf(bundle.getString("redis.port2")));

        List<JedisShardInfo> list = new LinkedList<JedisShardInfo>();
        list.add(jedisShardInfo1);
//        list.add(jedisShardInfo2);

        ShardedJedisPool pool = new ShardedJedisPool(config, list);

        // 从池中获取一个Jedis对象
        ShardedJedis jedis = pool.getResource();
        String keys = "name3";
        String value = "snowolf3";
        // 删数据
        jedis.del(keys);
        // 存数据
        jedis.set(keys, value);
        // 取数据
        String v = jedis.get(keys);

        System.out.println(v);

        // 释放对象池
        pool.returnResource(jedis);
    }
}
